<template>
    <div>
        <div style="height:30px;margin-top:3px">
            <Button type="primary" @click="createSs11002()" :disabled=isSendedHd
                    icon="md-add">保障人员录入
            </Button>
            <Button type="primary" class="step-item" style="margin-left:5px" @click="createSs11012()" :disabled=isSendedHd
                    icon="md-add">非保障人员录入
            </Button>

            <ss11002-modal ref="ss11002Modal" @ok="reloadData"></ss11002-modal>
            <ss11012-modal ref="ss11012Modal" @ok="reloadData"></ss11012-modal>
        </div>
        <div style="margin-top: 10px">
            <Table :columns="columns" :data="tableData" :loading="Loading" border>
                <template slot-scope="{ row, index}" slot="action">
                    <Button type="info" size="small" style="margin-right: 5px" @click="handleEdit(index)">编辑</Button>
                    <Button type="error" size="small" style="margin-right: 5px" :disabled=isSendedHd @click="handleDelete(index)">删除</Button>
                    <Button type="success" size="small" style="margin-right: 5px" @click="handleView(index)">详情</Button>
                    <Button v-if="row.azcp0517!='01' && row.type_name!='非保障人员'" :disabled=isSendedHd
                            type="warning" size="small" style="margin-right: 5px" @click="changeToHost(index)">设为户主
                    </Button>
                </template>
            </Table>
            <!--
            <vxe-table border
                       row-id="id"
                       :row-key="true"
                       show-overflow="tooltip"
                       highlight-hover-row
                       align="center"
                       header-align="center"
                       ref="xTree"
                       :loading="Loading"
                       :data="tableData">
                <vxe-table-column field="azcp0003" title="姓名" align="center"  />
                <vxe-table-column field="azcp0001" title="证件号码" align="center" />
                <vxe-table-column field="azcp0004_name" title="性别" align="center" />
                <vxe-table-column field="azcp0517_name" title="与申请人关系" align="center" />
                <vxe-table-column field="azcp0007_name" title="民族" align="center"/>
                <vxe-table-column field="azcp0009_name" title="健康状况" align="center"/>
                <vxe-table-column field="azcp0502_name" title="劳动能力丧失情况" align="center"/>
                <vxe-table-column field="type_name" title="保障状态" align="center" />
                <vxe-table-column field="" title="操作" min-width="100" fixed="right">
                    <template v-slot="{ row, index }">
                        <Button type="info" size="small" style="margin-right: 5px" @click="handleEdit(index)">编辑</Button>
                        <Button type="info" size="small" style="margin-right: 5px" @click="handleDelete(index)">删除</Button>
                        <Button type="success" size="small" style="margin-right: 5px" @click="handleView(index)">详情</Button>
                    </template>
                </vxe-table-column>
            </vxe-table>-->
            <div style="margin: 10px;overflow: hidden">
                <div style="float: right;">
                    <Page :total="dataCount" :page-size="pageSize" show-total class="paging"
                          @on-change="handleTableChange" :current="pageNo"></Page>
                </div>
            </div>
        </div>
        <div style="height:40px;line-height:40px;margin-top:10px;text-align: center ">
            <Button @click="prevStep" >上一步</Button>
            <Button type="primary" @click="nextStep"  :loading="btnLoading" style="margin-left:20px">下一步
            </Button>
        </div>
    </div>
</template>

<script>
  import ss11002Modal from './modules/Ss11002Modal.vue'
  import ss11012Modal from './modules/Ss11012Modal.vue'
  import { postAction } from '@/api/request'
  import { CommonMixin } from '@/views/common/js/CommonMixin'

  export default {
    name: 'application2',
    mixins: [CommonMixin],
    components: {
      ss11002Modal, ss11012Modal
    },
    data () {
      return {
          isSendedHd: false,
        url: {
          list: '/api/ac/subsistence/ss11002Service/getPageList',
          getSs11002One: '/api/ac/subsistence/ss11002Service/getOne',
          getSs11012One: '/api/ac/subsistence/ss11012Service/getOne',
          deleteSs11002: '/api/ac/subsistence/ss11002Service/deleteData',
          deleteSs11012: '/api/ac/subsistence/ss11012Service/deleteData',
          checkStep2:'/api/ac/subsistence/ss11002Service/checkPersonInfo',
        },
        dataCount: 0,
        pageSize: 10,
        familyInfo: {},
        queryParam: {},
        columns: [
          {
            title: '姓名',
            key: 'azcp0003',
            align: 'center',
              minWidth: 150
          },
          {
            title: '与申请人关系',
            key: 'azcp0517_name',
            align: 'center',
              minWidth: 150
          },
          {
            title: '性别',
            key: 'azcp0004_name',
            align: 'center',
              minWidth: 120
          },
          {
            title: '证件号码',
            key: 'azcp0001',
            align: 'center',
              minWidth: 200
          },
          {
            title: '民族',
            key: 'azcp0007_name',
            align: 'center',
              minWidth: 100
          },
          {
            title: '健康状况',
            key: 'azcp0009_name',
            align: 'center' ,
              minWidth: 150
          },
          {
            title: '劳动能力丧失情况',
            key: 'azcp0502_name',
            align: 'center',
              minWidth: 200
          },
          {
            title: '查验状态',
            key: 'azcp0578_name',
            align: 'center',
              minWidth: 150
          },
          {
            title: '保障状态',
            key: 'type_name',
            align: 'center',
            minWidth: 200
          },
          {
            title: '操作',
            slot: 'action',
            align: 'center',
            fixed:'right',
            minWidth:270
          }
        ],
        tableData: [],
        Loading: false,
        btnLoading:false,
        ss11001Id: this.$parent.familyInfo.ss11001Id,
        ss11002Id: this.$parent.familyInfo.ss11002Id,
      }
    },
    methods: {
      prevStep () {
        this.$emit('prevStep')
      },
      nextStep () {
        let that=this;
        that.btnLoading=true
        let url=that.url.checkStep2
        let dataParam={};
        dataParam.ssca0087=that.familyInfo.ssca0087;
        dataParam.ssca0002 =that.familyInfo.ssca0002;
        dataParam.azcp0500=that.ss11001Id;
        postAction(url, dataParam).then((res) => {
          if(res.data.data.code==1) {
            this.$emit('nextStep')
          } else {
            that.$Message.error(res.data.data.msg);
          }
        }).finally(() => {
          that.btnLoading=false
        });
      },
      handleEdit (index) {
        let that=this;
        let record=that.tableData[index];
        let url=null;
        let dataParam={};
        dataParam.id=record.id;
        dataParam.isSendedHd = that.isSendedHd;
        if (record.id) {
          if(record.type=='1'){
              that.$refs.ss11002Modal.isSendedHd = that.isSendedHd;
              url=that.url.getSs11002One;
            that.$refs.ss11002Modal.load(url, dataParam, that.familyInfo);
          }else{
              url=that.url.getSs11012One;
            that.$refs.ss11012Modal.load(url, dataParam);
          }
        } else {
          that.$Message.error('请选择操作数据');
        }
      },
      createSs11002 () { // 录入保障人员
        let that=this;
        that.$refs.ss11002Modal.ss11001Id=that.ss11001Id
        that.$refs.ss11002Modal.create()
      },
      createSs11012 () { // 录入非保障人员
        let that=this;
        that.$refs.ss11012Modal.ss11001Id=that.ss11001Id
        that.$refs.ss11012Modal.create()
      },
      handleDelete (index) {
        let that=this;
        let record=that.tableData[index];
        if (record.id) {
          let isMaster=record.azcp0517;
          if(isMaster=='01'){
            that.$Message.error('家庭户主不可删除，如需删除请更新家庭户主再操作');
            return
          }else{
            this.$Modal.confirm({
              title: '确定删除吗',
              onOk: () => {
                let url=null;
                let dataParam={};
                dataParam.id=record.id;
                if(record.type=='1'){
                  url=that.url.deleteSs11002
                }else{
                  url=that.url.deleteSs11012
                }
                postAction(url, dataParam).then((res) => {
                  if(res.data.data.code==1) {
                    that.$Message.success(res.data.data.msg);
                    that.reloadData();
                  } else {
                    that.$Message.error(res.data.data.msg);
                  }
                });
              },
              onCancel: () => {
              }
            });
          }

        } else {
          that.$Message.error('请选择操作数据');
        }
      },
        changeToHost(index) {
            let that = this;
            let record = that.tableData[index];
            if (record.id) {
                this.$Modal.confirm({
                    title: '确定变更为申请人',
                    onOk: () => {
                        postAction('/api/ac/subsistence/ss11002Service/createChangeToHost', record).then((res) => {
                            if (res.data.errcode=='0'){
                                that.$parent.familyInfo.azcp0001=record.azcp0001 //选中的家庭成员身份证号 作为 申请人身份证号
                            }
                            that.loadData();
                        });
                    },
                    onCancel: () => {
                    }
                });
            } else {
                that.$Message.error('请选择操作数据');
            }
        },
      handleView(index){
        let that=this;
        let record=that.tableData[index];
        let url=null;
        let dataParam={};
        that.Loading  =true
        dataParam.id=record.id;
        if (record.id) {
          if(record.type=='1'){
            url=that.url.getSs11002One
            postAction(url, dataParam).then((res) => {
              if(res.data.data.code==1) {
                res.data.data.data.ss12001Id = that.familyInfo.ss12001Id
                that.$refs.ss11002Modal.view(res.data.data.data)
              } else {
                that.$Message.error(res.data.data.msg);
              }
            });
          }else{
            url=that.url.getSs11012One
            postAction(url, dataParam).then((res) => {
              if(res.data.data.code==1) {
                that.$refs.ss11012Modal.view(res.data.data.data)
                  that.Loading = false
              } else {
                that.$Message.error(res.data.data.msg);
                  that.Loading = false
              }
            });
          }
        } else {
          that.$Message.error('请选择操作数据');
        }
          that.Loading = false
      },
      reloadData() {
        let that=this;
        that.queryParam.azcp0500=that.ss11001Id;
        that.loadData();
      },
    },
    created() {
      let that=this;
      that.familyInfo = that.$parent.familyInfo;
      that.queryParam.azcp0500=that.familyInfo.ss11001Id;
      that.isSendedHd = that.$parent.familyInfo.isSendedHd;
      this.loadData();
    },
  }
</script>

<style scoped>

</style>
